#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>


struct ListNode {
    int val;
    struct ListNode *next;
};
 
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2)
{
    int count = 0;
    struct ListNode* a = NULL;
    struct ListNode* b = NULL;
    while (l1 || l2)
    {
        int n1 = l1 ? l1->val : 0;
        int n2 = l2 ? l2->val : 0;
        int mid = n1 + n2 + count;
        if (!a)
        {
            a = b = malloc(sizeof(struct ListNode));
            b->val = mid % 10;
            b->next = NULL;
        }
        else
        {
            b->next = malloc(sizeof(struct ListNode));
            b->next->val = mid % 10;
            b = b->next;
            b->next = NULL;
        }
        count = mid / 10;
        if (l1)
        {
            l1 = l1->next;
        }
        if (l2)
        {
            l2 = l2->next;
        }

    }
    if (count > 0)
    {
        b->next = malloc(sizeof(struct ListNode));
        b->next->val = count;
        b->next->next = NULL;
    }
    return a;
}